<?php

namespace backend\controllers;



use common\models\Active;
use common\models\Address;
use common\models\Order;
use common\models\Shop;
use common\models\User;
use common\service\CommonService;
use common\service\ConstantService;
use common\service\GlobalUrlService;
use yii\base\Module;

class UserController extends BaseController
{

    /**
     * 列表页
     */
    public function actionUser(){
        if($this->isAjax() && $this->isGet()){
            $user_name = $this->get("user_name");
            $query = User::find();
            if(!empty($user_name)){
                $query->andWhere(['like','nickname',base64_encode($user_name)]);
            }
            $p = intval( $this->get('page',1) );
            $this->page_size = intval( $this->get('limit',10) );
            $p = ( $p > 0 )? $p : 1;
            $count = $query->count();

            $offset = ($p - 1) * $this->page_size;

            $users = $query->limit($this->page_size)->offset($offset)->asArray()->all();

            if(!empty($users)){
                $new_orders = [];
                $new_address = [];
                $user_ids = array_column($users,'id');

                $orders = Order::find()
                    ->select(['user_id','count(id) as order_num'])
                    ->where(['in','user_id',$user_ids])
                    ->groupBy('user_id')
                    ->asArray()
                    ->all();

                foreach ($orders as $order){
                    $new_orders[$order['user_id']] = $order;
                }
                $address_ids = array_column($users,'address_id');
                $address = Address::find()->where(['id'=>$address_ids])->asArray()->all();

                foreach ($address as $addre){
                    $new_address[$addre['id']] = $addre;
                }

                foreach ($users as &$user){
                    $user['nickname'] = base64_decode($user['nickname']);
                    $user['avatarurl'] = GlobalUrlService::buildCdnUrl($user['avatarurl']);
                    $user['order_num'] = 0;
                    $user['address'] = '';
                    $user['mobile'] = '';
                    if(isset($new_orders[$user['id']]['order_num']) && !empty($new_orders[$user['id']]['order_num']) ){
                        $user['order_num'] = $new_orders[$user['id']]['order_num'];
                    }

                    if(isset($new_address[$user['address_id']]) && !empty($new_address[$user['address_id']])){
                        $user['address'] = $new_address[$user['address_id']]['province']
                            .' '.$new_address[$user['address_id']]['city']
                            .' '.$new_address[$user['address_id']]['area']
                            .' '.$new_address[$user['address_id']]['district'];
                        $user['mobile'] = $new_address[$user['address_id']]['mobile'];
                    }

                }
            }
            return $this->renderPageJson($users,$count,ConstantService::$response_tab_success);
        }
        return $this->render('index');
    }

    /**
     * 用户详情页
     * @return string
     */
    public function actionSelect(){
        $id=$this->get('id');
        $data=User::find()->where(['id'=>$id])->asArray()->one();
        $data['nickname'] = base64_decode($data['nickname']);


        return $this->render('select',['data'=>$data]);
    }

    public function actionGetOrder(){
        if($this->isGet() && $this->isAjax()){
            $id = $this->get('id');

            $query=Order::find();

            $query->andWhere(['user_id'=>$id]);
            $count  = $query->count();
            $order=$query->asArray()->all();

            if(!empty($order)){
                $shop_ids = array_column($order,'shop_id');
                $shops = Shop::find()
                    ->select(['id','shop_name'])
                    ->where(['id'=>$shop_ids])
                    ->asArray()
                    ->all();

                $new_shops = [];
                foreach ($shops as $shop){
                    $new_shops[$shop['id']] = $shop;
                }
                unset($shop);

                $activity_ids = array_column($order,'active_id');
                $activities = Active::find()
                    ->select(['id','active_name'])
                    ->where(['id'=>$activity_ids])
                    ->asArray()
                    ->all();

                $new_activities = [];
                foreach ($activities as $active){
                    $new_activities[$active['id']] = $active;
                }

                foreach ($order as &$value){
                    $value['shop_name'] = '';
                    if( isset($new_shops[$value['shop_id']]) && !empty($new_shops[$value['shop_id']]) && isset($new_shops[$value['shop_id']]) ){
                        $value['shop_name'] = $new_shops[$value['shop_id']]['shop_name'];
                    }
                    $value['active_name'] = '';
                    if(isset($new_activities[$value['active_id']])&& !empty($new_activities[$value['active_id']])){
                        $value['active_name'] = $new_activities[$value['active_id']]['active_name'];
                    }
                    if(!empty($value['complete_time'])){
                        $value['complete_time'] = date('Y-m-d H:i:s',$value['complete_time']);
                    }
                }
                unset($value);
            }
            return $this->renderPageJson($order,$count,ConstantService::$response_tab_success);
        }


    }

    public function actionGetAddress(){
        if($this->isGet() && $this->isAjax()){
            $user_id = $this->get('id');
            $address=Address::find()->where(['user_id'=>$user_id])->asArray()->all();
            return $this->renderPageJson($address,count($address),ConstantService::$response_tab_success);
        }

    }

}